//
//  HomeLayer.cpp
//  IceCreamSundae
//
//  Created by luotianqiang1 on 16/5/23.
//
//

#include "HomeLayer.h"
#include "LQ_adLoadingLayer.h"
REGIST_SECENE(HomeLayer)
bool HomeLayer::init(){
    if(BaseHomeLayer::init()) {
        startClass = "MakeStep1Layer";
        _operate = StudioLayer::create("HomeLayer.csb");
        addChild(_operate);
        
        auto hideNodes = {"logo","btn_play","btn_more","food0","food1"};
        for(auto name:hideNodes)
        _operate->getNodeByName(name)->setVisible(false);
        
        return true;
    }
    return false;
}

void HomeLayer::touchEnd(ui::Widget* widget){
    BaseHomeLayer::touchEnd(widget);
}

void HomeLayer::onExitTransitionDidStart(){
    BaseHomeLayer::onExitTransitionDidStart();
}

void HomeLayer::onEnter(){
    BaseHomeLayer::onEnter();
    AdsManager::getInstance()->showAds(ADS_TYPE::kTypeBannerAds);
}

void HomeLayer::onEnterTransitionDidFinish(){
    BaseHomeLayer::onEnterTransitionDidFinish();
    SoundPlayer::getInstance()->playBackGroundMusic("sound/bgm.mp3");
    auto food = _operate->getNodeByName("food0");
    auto pos = food->getPosition();
    food->setPosition(pos+Vec2(200,60));
    food->setScale(0);
    food->setVisible(true);
    food->runAction(ScaleTo::create(1, 1));
    food->runAction(MoveTo::create(1, pos));
    
    auto food1 = _operate->getNodeByName("food1");
    pos = food1->getPosition();
    food1->setPosition(pos+Vec2(300,100));
    food1->setScale(0);
    food1->setVisible(true);
    food1->runAction(Sequence::create(JumpTo::create(1, pos, 280, 1),ActionHelper::getScaleBtnAction(), nullptr));
    food1->runAction(ScaleTo::create(1, 1));
    
    
    ActionHelper::delayFunc(1, this, [this](){
        auto logo = _operate->getNodeByName("logo");
        logo->setVisible(true);
        logo->setScale(0.4);
        logo->runAction(ScaleTo::create(1, 1));
        auto moveVec2 = Vec2(400,150);
        logo->setPosition(logo->getPosition()+moveVec2);
        SoundPlayer::getInstance()->playEffect("sound/general/award stick_in.mp3");
        logo->runAction(EaseExponentialOut::create(MoveBy::create(1, -moveVec2)));
        logo->runAction(EaseExponentialOut::create(RotateBy::create(1, 360*2)));
        ActionHelper::delayFunc(0.8, this, [=](){
            SoundPlayer::getInstance()->playEffect("sound/general/ingredients_fly_in.mp3");
            auto btn_play = _operate->getNodeByName("btn_play");
            btn_play->setRotation(-600);
            btn_play->setScale(0);
            btn_play->setVisible(true);
            btn_play->runAction(Sequence::create(ScaleTo::create(0.7, 1),CallFunc::create([=](){
                _operate->getNodeByName("logo")->runAction(RepeatForever::create(Sequence::create(MoveBy::create(0.9, Vec2(0,10)), MoveBy::create(0.9, Vec2(0,-10)),  nullptr)));
                btn_play->runAction(RepeatForever::create(Sequence::create(ScaleTo::create(0.9, 1.1),ScaleTo::create(0.9, 0.9), nullptr)));
            }), nullptr));
            btn_play->runAction(RotateBy::create(0.7, 600));
            
            auto btn_more = _operate->getNodeByName("btn_more");
            btn_more->setPosition(btn_more->getPosition()-Vec2(0,400));
            btn_more->setVisible(true);
            btn_more->runAction(JumpBy::create(0.9, Vec2(0,400), 500, 1));
        });
    });
   
    
    SoundPlayer::getInstance()->playEffect("sound/general/ingredients_fly_in.mp3");
}

void HomeLayer::showAdsCallBack(){
    LQ_adLoadingLayer::showLoading<LQ_adLoadingLayer>(true);
    if(IAPManager::getInstance()->isShowAds())
    AdsManager::getInstance()->showAds(ADS_TYPE::kTypeBannerAds);
}

void  HomeLayer::playClick(){
    _operate->setIsChange(true);
    BaseHomeLayer::playClick();
}
